const _jpush = module.exports

const JPush = require('jpush-async/lib/JPush/JPushAsync')
const appCfg = configure.get('app')

// 推送通知
_jpush.send = async (receiver_id, clientInfo) => {
    if (!_.trim(receiver_id)) throw new Error('receiver_id不能为空')

    if (!clientInfo.app_key) throw new Error('app_key不能为空')
    if (!clientInfo.app_secret) throw new Error('app_secret不能为空')

    let client = JPush.buildClient(clientInfo.app_key, clientInfo.app_secret)

    let result = await client.push()
        .setPlatform(JPush.ALL)
        .setOptions(null,null,null,appCfg.apns_production,null,null)
        .setAudience(null, JPush.alias(receiver_id)) //注册id
        //消息
        .setNotification(
            JPush.ios(_.get(clientInfo,'content.alert') || '', _.get(clientInfo,'content.sound') || 'default', null, false, _.get(clientInfo,'content') || {})
        )
        .setMessage(JSON.stringify(_.get(clientInfo,'content') || {}))
        .send()

    log4js.debug('_jpush.send ======', result)

    return result
}



